/*******************************************************************************
 * Copyright (c) 2007 University of Oslo.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     University of Oslo - initial API and implementation
 *******************************************************************************/

package com.cea.papyrus.diagram.sequence.sedi.model.command;

import static com.cea.papyrus.diagram.sequence.sedi.model.util.ModelUtil.getBodies;

import java.util.List;

import org.eclipse.gef.commands.Command;
import org.eclipse.uml2.uml.Lifeline;
import org.eclipse.uml2.uml.OpaqueExpression;
import org.eclipse.uml2.uml.UMLFactory;
import org.eclipse.uml2.uml.ValueSpecification;

import com.cea.papyrus.diagram.sequence.sedi.model.util.OpaqueExpressionUtil;

/**
 * Command to set the selector expression for a Lifeline.
 * 
 * <p>
 * The following attributes must be set for the command to complete
 * successfully:
 * </p>
 * <ul>
 * <li>Lifeline
 * <li>SelectorExpression
 * </ul>
 * 
 * <p>
 * <b>Known limitations</b> The selector is always constructed as an
 * OpaqueExpression, and the expression string is always stored in the body #0.
 * </p>
 * 
 * @author frankd@ifi.uio.no
 */
public class SetSelectorCommand extends Command {
	private Lifeline lifeline;
	private String selectorExpr, oldSelectorExpr;
	private ValueSpecification oldSelector;
	private boolean addedBody;

	public SetSelectorCommand() {
	}

	public SetSelectorCommand(final Lifeline lifeline, final String selectorExpr) {
		setLifeline(lifeline);
		setSelectorExpression(selectorExpr);
	}

	public void setLifeline(final Lifeline lifeline) {
		this.lifeline = lifeline;
	}

	public void setSelectorExpression(final String selectorExpr) {
		this.selectorExpr = selectorExpr;
	}

	@Override
	public void execute() {
		if (selectorExpr == null) {
			oldSelector = lifeline.getSelector();
			if (oldSelector != null)
				lifeline.setSelector(null);
		} else {
			if (lifeline.getSelector() instanceof OpaqueExpression) {
				OpaqueExpression opaqueExpr = (OpaqueExpression) lifeline
						.getSelector();
				List<String> bodies = getBodies(opaqueExpr);
				if (bodies.isEmpty()) {
					OpaqueExpressionUtil.addBody(opaqueExpr, selectorExpr);
					addedBody = true;
				} else {
					oldSelectorExpr = bodies.get(0);
					bodies.set(0, selectorExpr);
				}
			} else {
				OpaqueExpression opaqueExpr = UMLFactory.eINSTANCE
						.createOpaqueExpression();
				opaqueExpr.setName("LifelineSelector");
				OpaqueExpressionUtil.addBody(opaqueExpr, selectorExpr);
				lifeline.setSelector(opaqueExpr);
			}
		}
	}

	@Override
	public void undo() {
		if (oldSelectorExpr != null) {
			OpaqueExpression opaqueExpr = (OpaqueExpression) lifeline
					.getSelector();
			if (opaqueExpr != null)
				getBodies(opaqueExpr).set(0, oldSelectorExpr);
			else
				lifeline.setSelector(oldSelector);
		} else if (oldSelector != null)
			lifeline.setSelector(oldSelector);
		else if (addedBody) {
			OpaqueExpression opaqueExpr = (OpaqueExpression) lifeline
					.getSelector();
			List<String> bodies = getBodies(opaqueExpr);
			OpaqueExpressionUtil.removeBody(opaqueExpr, bodies.size() - 1);
			addedBody = false;
		} else
			lifeline.setSelector(null);

		oldSelectorExpr = null;
		oldSelector = null;
	}

	@Override
	public void redo() {
		this.execute();
	}

	@Override
	public void dispose() {
		lifeline = null;
		selectorExpr = null;
		oldSelectorExpr = null;
		oldSelector = null;
	}
}
